﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;

namespace WebSite.UserControl
{
    public partial class Search : System.Web.UI.UserControl
    {
        public string serverpath = System.Configuration.ConfigurationManager.AppSettings["ServerPath"] + "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadContent();
            }
        }

        #region 加载内容
        private void LoadContent()
        {
            string keyword = Request.QueryString["keyword"] + "";
            //搜索框中的内容
            //txtContent.Text = Session["keyword"]+"";
            txtContent.Text = keyword;

            string content = "";

            string url = Request.Url.ToString();//当前url
            string serverpath = System.Configuration.ConfigurationManager.AppSettings["ServerPath"] + "/";
            string pageurl=url.Replace(serverpath,"");
            if (pageurl.Contains("/"))//子页面
            {
                string[] array = pageurl.Split('/');
                string typename = array[0];

                string getid = Request.QueryString["id"] + "";
                string id = getid;
                //查找该ID属于用户
                if (typename.ToLower() == "b")
                {
                    Blog binfo = new Blog();
                    BlogHelper bhelper = new BlogHelper();
                    binfo = bhelper.GetBlog(id);
                    string bloguserid = binfo.UsersID;
                    string nickname = binfo.NickName;
                    content = "<a href=\"../Index.aspx\">首页</a>&gt;";
                    content += "<a href=\"../u/" + bloguserid + ".aspx\">" + nickname + "</a>&gt;正文";
                }
                else if (typename.ToLower() == "u")//个人页面
                {
                    //string getid = Request.QueryString["id"] + "";
                    //string id = getid = getid.Substring(0, getid.Length - 1);//用户ID
                    Users uinfo = new Users();
                    UsersHelper uhelper = new UsersHelper();
                    id = uhelper.GetUsersID(id);
                    uinfo = uhelper.GetUsers(id);
                    string nickname = uinfo.NickName;
                    content = "<a href=\"../Index.aspx\">首页</a>&gt;";
                    content += "<a href=\"" + id + ".aspx\">" + nickname + "</a>";
                }
                else if (typename.ToLower() == "c")//公司页面
                {
                    Companys cinfo = new Companys();
                    CompanysHelper chelper = new CompanysHelper();
                    id = chelper.GetCompanyID(id);
                    cinfo = chelper.GetCompanys(id);
                    string companyname = cinfo.CompanyName;
                    string realmname = cinfo.RealmName;
                    content = "<a href=\"../Index.aspx\">首页</a>&gt;";
                    content += "<a href=\"" + id + ".aspx\">" + companyname + "</a>";
                }
                else if (typename.ToLower() == "cate")//按类别查找
                {
                    BlogType btinfo = new BlogType();
                    BlogTypeHelper bthelper = new BlogTypeHelper();
                    btinfo = bthelper.GetBlogType(id);
                    string blogtypename = btinfo.BlogTypeName;
                    content = "<a href=\"../Index.aspx\">首页</a>&gt;";
                    content += "<a href=\"" + id + ".aspx\">" + blogtypename + "</a>";
                }
                else if (pageurl.Contains("BeCareList"))//关注他的用户
                {
                    content = "<a href=\"../Index.aspx\">首页</a>&gt;";
                    content += "关注他的用户";
                }
                else if (pageurl.Contains("CareList"))//他关注的用户
                {
                    content = "<a href=\"../Index.aspx\">首页</a>&gt;";
                    content += "他关注的用户";
                }
                
                
            }
            else//首页
            {
                //加载后台首页搜索设置的内容
                News ninfo = new News();
                NewsHelper nhelper = new NewsHelper();
                Criteria c = new Criteria();
                c.Add(CriteriaType.Equals, "NewsTypeID", "indexsearch");
                c.Add(CriteriaType.Equals, "Flag", 0);

                List<News> nlist = nhelper.QueryNewsBySearch(c, new Order[] { new Order("CreateDate", OrderMode.Desc) });
                if (nlist != null && nlist.Count > 0)
                {
                    for (int i = 0; i < nlist.Count; i++)
                    {
                        ninfo = nlist[i];
                        string gettitle = ninfo.Title;//中间用；隔开 前面为标题，后面为该标题的链接
                        if (gettitle.Contains(";"))
                        {
                            string[] strspit = gettitle.Split(';');
                            string title = strspit[0];
                            string linkurl = strspit[1];

                            content += "<a href=\""+linkurl+"\" target=\"_blank\">"+title+"</a>";
                            content += "&nbsp;&nbsp;";
                        }
                    }
                }
            }

            litContent.Text = content;
        }
        #endregion

        #region 搜索
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string keyword = txtContent.Text.Trim();
            //Session["keyword"] = keyword;
            
            Response.Redirect(serverpath+"/SearchResult.aspx?keyword="+keyword);
            
        }
        #endregion
    }
}